package com.example.myapplication;

import android.app.Dialog;
import android.os.Bundle;

import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import q.rorbin.verticaltablayout.VerticalTabLayout;
import q.rorbin.verticaltablayout.widget.QTabView;
import q.rorbin.verticaltablayout.widget.TabView;

public class RecyclerActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    VerticalTabLayout tabLayout;
    List<TestData> list;
    ImageButton car;
    private int currentPosition = 0;
    private LinearLayoutManager llm;
    private int recyclerHeight;

    private VerticalTabLayout.OnTabSelectedListener tabSelectedListener;

    private String[] typename = {"早餐堡","汉堡","炸鸡","饮品","冰淇淋","沙拉"};

    private int[][] foodimg = {{R.drawable.brf1,R.drawable.brf2,R.drawable.brf3,R.drawable.brf4,R.drawable.brf5},
            {R.drawable.burger1,R.drawable.burger2,R.drawable.burger3},
            {R.drawable.meat1,R.drawable.meat2},
            {R.drawable.drink1,R.drawable.drink2,R.drawable.drink3,R.drawable.drink4},
            {R.drawable.ice1,R.drawable.ice2},
            {R.drawable.salad1,R.drawable.salad2,R.drawable.salad3}
    };
    private String[][] foodname= {{"芝士培根蛋堡","奶油猪肉堡","奶油猪肉芝士堡","芝士牛肉堡","芝士牛肉蛋堡"}
        ,{"三层牛肉巨无霸","芝士双牛堡","奥尔良烤鸡腿堡"}
        ,{"香辣鸡翅","新奥尔良鸡翅"}
        ,{"可乐","奶茶","酸梅汁","咖啡"}
        ,{"炫迈冰淇淋","巧克力冰淇淋"}
        ,{"猪肉拌沙拉","鸡胸肉拌沙拉","玉米沙拉"}
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler);
        recyclerView = findViewById(R.id.recycler_view);
        tabLayout = findViewById(R.id.tab_layout);

        car = findViewById(R.id.car);
        car.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Dialog dialog = new Dialog(RecyclerActivity.this,R.style.Theme_AppCompat_Dialog);
                dialog.setContentView(LayoutInflater.from(RecyclerActivity.this).inflate(R.layout.dialog, null));
                Window dialogWindow = dialog.getWindow();
                dialogWindow.getDecorView().setPadding(0, 0, 0, 0);// 边距设为0
                dialogWindow.setBackgroundDrawableResource(android.R.color.transparent);//背景透明，不然会有个白色的东东
                dialogWindow.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口弹出动画
                WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度
                lp.height = 300; // 高度
                dialogWindow.setAttributes(lp);
                dialogWindow.setGravity(Gravity.BOTTOM);
                // 弹出dialog
                dialog.show();
            }
        });
        initData();
        initView();
    }

    private void initData() {
        list = new ArrayList<>();
        for (int i = 0; i < foodimg.length; i++) {
            TestData testData = new TestData();
            List<Fruit> itemList = new ArrayList<>();

            for (int j = 0; j < foodimg[i].length; j++) {
                Fruit fruit = new Fruit(foodname[i][j],foodimg[i][j],"￥22",R.drawable.plus);
                itemList.add(fruit);
            }
            testData.setItemName(itemList);
            testData.setName(typename[i]);
            list.add(testData);
            tabLayout.addTab(new QTabView(getBaseContext()).setTitle(
                    new QTabView.TabTitle.Builder().setContent(testData.getName()).build()));
        }
    }

    private void initView(){
        llm = new LinearLayoutManager(getBaseContext());
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(llm);

        /**
         *getHeight 需延时获取，有网络请求时可响应结束后同步获取，否则异步获取
         */
        recyclerView.post(new Runnable() {
            @Override
            public void run() {
                recyclerHeight = recyclerView.getHeight();
                Log.d("recycleHeight",recyclerHeight + "");
                LinerRecycleAdapter adapter = new LinerRecycleAdapter(getBaseContext(),list,recyclerHeight);
                recyclerView.setAdapter(adapter);
            }
        });


        tabSelectedListener = new VerticalTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabView tab, int position) {
                if (currentPosition != position){
                    llm.scrollToPositionWithOffset(position,0);
                    currentPosition = position;
                }
            }

            @Override
            public void onTabReselected(TabView tab, int position) {

            }
        };

        RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                for (int i= 0; i < llm.getItemCount();i++){
                    if (null != llm.findViewByPosition(i)){
                        if (llm.findViewByPosition(i).getTop() < recyclerHeight / 2 && llm.findViewByPosition(i).getBottom() > recyclerHeight / 2){
                            if (tabLayout.getSelectedTabPosition() != i){
                                currentPosition = i;
                                tabLayout.setTabSelected(i);
                            }
                        }
                    }
                }
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == 0){
                    tabLayout.removeOnTabSelectedListener(tabSelectedListener);
                }else if (newState == 3){
                    tabLayout.removeOnTabSelectedListener(tabSelectedListener);
                }else {
                    tabLayout.addOnTabSelectedListener(tabSelectedListener);
                }
            }
        };
        tabLayout.addOnTabSelectedListener(tabSelectedListener);
        recyclerView.addOnScrollListener(onScrollListener);
    }
}
